﻿/*++
 * Copyright (c) DemeterSoft Corporation
 *
 * File Name:
 *     SlView.cs
 *     
 * Abstract:
 *     SlView视图，能存放在SlViewContainer中的视图都需要继承此类
 *
 * Revision History:
 *     08/24/2011    yliu
 *         创建文件
 * --*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SmartSpace.Silverlight
{
    public delegate void ViewHideEventHandler(SlView sender, bool isCancel);
    public delegate void ViewShowEventHandler(SlView sender, bool isFirstShow);

    /// <summary>
    /// SlView视图，能存放在SlViewContainer中的视图都需要继承此类
    /// </summary>
    public partial class SlView : UserControl
    {
        #region fields

        /// <summary>
        /// 是否是提交，提交时确认退出直接返回true
        /// </summary>
        private bool m_isCanceled = true;

        /// <summary>
        /// 视图容器
        /// </summary>
        internal SlViewContainer ViewContainer
        {
            get;
            //call by: SlViewContainer
            set;
        }

        /// <summary>
        /// 父视图
        /// </summary>
        internal SlView ParentView
        {
            get;
            // call by: SlView.SwitchView()
            set;
        }

        /// <summary>
        /// 第一次显示
        /// </summary>
        protected bool IsFirstDisplay { get; private set; }

        #endregion

        /// <summary>
        /// 视图被隐藏时的事件
        /// </summary>
        public event ViewHideEventHandler Hide;

        public event ViewShowEventHandler Show;

        public event Action<object> CustomEvent;

        /// <summary>
        /// 认证信息
        /// </summary>
        public Authentication Authentication { get; set; }

        /// <summary>
        /// DemeterSoft Silverlight Header
        /// </summary>
        public SlView()
        {
            IsFirstDisplay = true;

            this.Background = new SolidColorBrush(Colors.Brown);
            this.MouseRightButtonDown += new MouseButtonEventHandler(SlView_MouseRightButtonDown);
        }

        void SlView_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
//#if Debug
            if (e.OriginalSource == this)
            {
                MessageBox.Show(this.GetType().FullName);
            }
//#endif
            e.Handled = true;
        }

        /// <summary>
        /// 切换子视图
        /// </summary>
        /// <param name="subView">子视图对象</param>
        public void SwitchView(SlView subView)
        {
            subView.ParentView = this;
            subView.Authentication = this.Authentication;
            ViewContainer.SwitchSubView(this, subView);
        }

        /// <summary>
        /// 返回到上一视图
        /// </summary>
        public void GoBack()
        {
            ViewContainer.GoBack();
        }

        public void PostCustomEvent(object obj)
        {
            if (CustomEvent != null)
            {
                CustomEvent(obj);
            }
        }

        /// <summary>
        /// 获得视图的标题
        /// </summary>
        /// <returns></returns>
        public virtual string GetCaption()
        {
            return this.ToString();
        }

        /// <summary>
        /// 视图切换完毕
        /// call by: SlViewContainer
        /// </summary>
        /// <param name="inOrOut">true:in, false:out</param>
        internal void SwitchComplated(bool inOrOut)
        {
            if (inOrOut)
            {
                OnShown();
            }
            else
            {
                OnHidden();
            }

            if (inOrOut && Show != null)
            {
                Show(this, IsFirstDisplay);
            }

            if (!inOrOut && Hide != null)
            {
                Hide(this, m_isCanceled);
            }

            if (inOrOut && IsFirstDisplay)
            {
                IsFirstDisplay = false;
            }
        }

        protected virtual void OnShown()
        {
        }

        protected virtual void OnHidden()
        {
        }

        /// <summary>
        /// 回退
        /// </summary>
        public void Cancel()
        {
            m_isCanceled = true;
            ViewContainer.GoBack();
        }

        /// <summary>
        /// 提交
        /// </summary>
        public void Submit()
        {
            m_isCanceled = false;
            ViewContainer.GoBack();
        }

        /// <summary>
        /// 退出确定
        /// </summary>
        /// <returns></returns>
        internal bool ConfirmExit()
        {
            if (!m_isCanceled)
            {
                return true;
            }
            return OnConfirmCancel();
        }

        /// <summary>
        /// 确认是否退出的调用的方法
        /// </summary>
        /// <returns></returns>
        protected virtual bool OnConfirmCancel()
        {
            return true;
        }

        public void ShowMessage(string message)
        {
            ViewContainer.ShowMessage(message);
        }
    }
}
